//                        date: [[20120806020642]]
//                        Author: Ahmed Fathi Hamouda Abd El Hafez
using System;
using System.ComponentModel;
using System.Reflection;
using System.Data;
using System.Collections;
using System.Runtime.Serialization;
using System.Diagnostics;
using CustomersSuppliersDataLayer;
using System.Collections.Generic;


namespace CustomersSuppliersBusinessLayer
{

    /// <summary>
    ///  BusinessCollection Object OF Table Importance.
    /// </summary>

    [Serializable]
    public partial class BLImportanceCollection : BindingList<BLImportance>
    {
        #region fields And Prperties

        private BLImportance f_Template = new BLImportance();
        public BLImportance Template
        {
            get
            {
                return f_Template;
            }
            set
            {
                f_Template = value;
            }
        }

        /// <summary>
        /// get the error ocured after insert or update or delete or load
        /// </summary>
        public Exception Error
        {
            get { return this.f_Template.Error; }
        }


        #endregion


        #region Constructors


        /// <summary>
        ///  initialize new BLImportanceCollection
        /// </summary>
        public BLImportanceCollection()
        {

        }


        /// <summary>
        ///  initialize new BLImportanceCollection and adding the items
        /// </summary>
        public BLImportanceCollection(IEnumerable<BLImportance> Items)
            : this()
        {
            this.AddRange(Items);
        }


        /// <summary>
        ///  initialize new BLImportanceCollection and load data acording to filter 
        /// </summary>
        public BLImportanceCollection(BLImportance template)
        {
            this.f_Template = template;
            this.Load();
        }


        #endregion


        #region methods
        /// <summary>
        ///  return the count of the collection after adding range
        /// </summary>
        public int AddRange(IEnumerable<BLImportance> collection)
        {
            foreach (var item in collection)
            {
                this.Add(item);
            }
            return this.Count;
        }



        /// <summary>
        ///Update the the dataBase using template object
        /// <summary>
        public virtual int Update(string[] properties, string[] autoDateProperties)
        {
            return this.f_Template.Update(properties, autoDateProperties);
        }
        /// <summary>
        ///Update the the dataBase using template object with setting
        /// <summary>
        public virtual int Update(string[] properties, string[] autoDateProperties, Settings setting)
        {
            return this.f_Template.Update(properties, autoDateProperties, setting);
        }
        /// <summary>
        /// Update the dataBase using template object by some properties it has 
        /// </summary>
        public virtual int UpdateByProperties(string[] properties, string[] autoDateProperties, string[] filterProperties)
        {
            return this.f_Template.UpdateByProperties(properties, autoDateProperties, filterProperties);
        }
        /// <summary>
        /// Update the dataBase using template object by some properties it has using settings
        /// </summary>
        public virtual int UpdateByProperties(string[] properties, string[] autoDateProperties, string[] filterProperties, Settings setting)
        {
            return this.f_Template.UpdateByProperties(properties, autoDateProperties, filterProperties, setting);
        }



        /// <summary>
        ///Delete the the dataBase using template object
        /// <summary>
        public virtual int Delete()
        {
            return this.f_Template.Delete();
        }
        /// <summary>
        ///Delete the the dataBase using template object with setting
        /// <summary>
        public virtual int Delete(Settings setting)
        {
            return this.f_Template.Delete(setting);
        }
        /// <summary>
        /// Delete the dataBase using template object by some properties it has 
        /// </summary>
        public virtual int DeleteByProperties(string[] filterProperties)
        {
            return this.f_Template.DeleteByProperties(filterProperties);
        }
        /// <summary>
        /// Delete the dataBase using template object by some properties it has using settings
        /// </summary>
        public virtual int DeleteByProperties(string[] filterProperties, Settings setting)
        {
            return this.f_Template.DeleteByProperties(filterProperties, setting);
        }



        /// <summary>
        ///  load collection using dataBaseFilterObject
        /// </summary>
        public void Load()
        {
            this.ClearItems();
            if (this.f_Template == null)
            {
                f_Template = new BLImportance();
            }
            DLImportance[] items = this.f_Template.DLImportance.Select(null, null);
            for (int i = 0; i < items.Length; i++)
            {
                this.Add(new BLImportance(items[i], BusinessObjectState.Loaded));
            }
        }

        /// <summary>
        ///  load collection using dataBaseFilterObject with settings
        /// </summary>
        public void Load(Settings setting)
        {
            this.ClearItems();
            if (this.f_Template == null)
            {
                f_Template = new BLImportance();
            }
            DLImportance[] items = this.f_Template.DLImportance.Select(null, null, setting);
            for (int i = 0; i < items.Length; i++)
            {
                this.Add(new BLImportance(items[i], BusinessObjectState.Loaded));
            }
        }


        /// <summary>
        ///  load collection using filter properties
        /// </summary>
        public void LoadByProperties(string[] filterProperties)
        {
            this.ClearItems();
            if (this.f_Template == null)
            {
                this.f_Template = new BLImportance();
            }
            this.f_Template.Filters.Clear();
            for (int i = 0; i < filterProperties.Length; i++)
            {
                Type t = this.f_Template.GetType().GetProperty(filterProperties[i], BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase).PropertyType;
                CustomersSuppliersDataLayer.DataFilter filter = new DataFilter(LogicalOperators.And, filterProperties[i], FilterOperators.Equal, this.f_Template[filterProperties[i]],t);
                this.f_Template.Filters.Add(filter);
            }

            DLImportance[] items = this.f_Template.DLImportance.Select(null, null);
            for (int i = 0; i < items.Length; i++)
            {
                this.Add(new BLImportance(items[i], BusinessObjectState.Loaded));
            }
        }

        /// <summary>
        ///  load collection using filter properties with settings
        /// </summary>
        public void LoadByProperties(string[] filterProperties, Settings setting)
        {
            this.ClearItems();
            if (this.f_Template == null)
            {
                f_Template = new BLImportance();
            }
            this.f_Template.Filters.Clear();
            for (int i = 0; i < filterProperties.Length; i++)
            {
                Type t=f_Template.GetType().GetProperty(filterProperties[i], BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase).PropertyType;
                CustomersSuppliersDataLayer.DataFilter filter = new DataFilter(LogicalOperators.And, filterProperties[i], FilterOperators.Equal, this.f_Template[filterProperties[i]], t);
                this.f_Template.Filters.Add(filter);
            }

            DLImportance[] items = this.f_Template.DLImportance.Select(null, null, setting);
            for (int i = 0; i < items.Length; i++)
            {
                this.Add(new BLImportance(items[i], BusinessObjectState.Loaded));
            }
        }

        /// <summary>
        ///  load collection using dataBaseFilterObject with start and end
        /// </summary>
        public void Load(int? start, int? end)
        {
            this.ClearItems();
            if (this.f_Template == null)
            {
                f_Template = new BLImportance();
            }
            DLImportance[] items = this.f_Template.DLImportance.Select(start, end);
            for (int i = 0; i < items.Length; i++)
            {
                this.Add(new BLImportance(items[i], BusinessObjectState.Loaded));
            }
        }

        /// <summary>
        ///  load collection using dataBaseFilterObject with start and end
        /// </summary>
        public void Load(int? start, int? end, Settings setting)
        {
            this.ClearItems();
            if (this.f_Template == null)
            {
                f_Template = new BLImportance();
            }
            DLImportance[] items = this.f_Template.DLImportance.Select(start, end, setting);
            for (int i = 0; i < items.Length; i++)
            {
                this.Add(new BLImportance(items[i], BusinessObjectState.Loaded));
            }
        }

        /// <summary>
        ///  load collection using filter properties in the range of start and end 
        /// </summary>
        public void LoadByProperties(int? start, int? end, string[] filterProperties)
        {
            this.ClearItems();
            if (this.f_Template == null)
            {
                f_Template = new BLImportance();
            }
            this.f_Template.Filters.Clear();
            for (int i = 0; i < filterProperties.Length; i++)
            {
                Type t = f_Template.GetType().GetProperty(filterProperties[i], BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase).PropertyType;
                CustomersSuppliersDataLayer.DataFilter filter = new DataFilter(LogicalOperators.And, filterProperties[i], FilterOperators.Equal, this.f_Template[filterProperties[i]], t);
                this.f_Template.Filters.Add(filter);
            }
            DLImportance[] items = this.f_Template.DLImportance.Select(start, end);
            for (int i = 0; i < items.Length; i++)
            {
                this.Add(new BLImportance(items[i], BusinessObjectState.Loaded));
            }
        }

        /// <summary>
        ///  load collection using filter properties in the range of start and end with settings
        /// </summary>
        public void LoadByProperties(int? start, int? end, string[] filterProperties, Settings setting)
        {
            this.ClearItems();
            if (this.f_Template == null)
            {
                f_Template = new BLImportance();
            }
            this.f_Template.Filters.Clear();
            for (int i = 0; i < filterProperties.Length; i++)
            {
                Type t = f_Template.GetType().GetProperty(filterProperties[i], BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase).PropertyType;
                CustomersSuppliersDataLayer.DataFilter filter = new DataFilter(LogicalOperators.And, filterProperties[i], FilterOperators.Equal, this.f_Template[filterProperties[i]], t);
                this.f_Template.Filters.Add(filter);
            }
            DLImportance[] items = this.f_Template.DLImportance.Select(start, end, setting);
            for (int i = 0; i < items.Length; i++)
            {
                this.Add(new BLImportance(items[i], BusinessObjectState.Loaded));
            }
        }



        /// <summary>
        ///  auto save all individual objects
        /// </summary>
        public void AutoSave()
        {
            for (int i = 0; i < this.Count; i++)
            {
                this[i].AutoSave();
            }
        }

        /// <summary>
        ///  auto save all individual objects using setting
        /// </summary>
        public void AutoSave(Settings setting)
        {
            for (int i = 0; i < this.Count; i++)
            {
                this[i].AutoSave(setting);
            }
        }


        /// <summary>
        ///  return the position of the specified item with its identifier eqal these values
        /// </summary>
        public int FindByIentifier(
        Guid? Importance_ID
)
        {
            int index = -1;
            for (int i = 0; i < this.Count; i++)
            {
                bool iseqal = true;
                if (this[i].Importance_ID != Importance_ID)
                {
                    iseqal = false;
                    continue;
                }
                if (iseqal)
                {
                    index = i;
                    break;
                }
            }
            return index;
        }

        #endregion

    }
}